/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;
import POJO.NhaHang;
import UTIL.HibernateUtil;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author Administrator
 */
public class NhaHangDAO {
    
    /**
     * @desc lay danh sach nha hang
     * @return 
     */
    public static List<NhaHang> layDanhSach() {
        List<NhaHang> ds = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            String hql = "select nh from NhaHang nh";
            org.hibernate.Query query = session.createQuery(hql);
            ds = query.list();
        } catch (HibernateException ex) {
            System.err.println(ex);
        } finally {
            session.close();
        }
        return ds;  
    }
    
    /**
     * @desc tim nha hang theo ma nha hang
     * @param maNhaHang
     * @return 
     */
    public static NhaHang layThongTinNhaHang(int maNhaHang) {
        NhaHang nh = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            nh = (NhaHang) session.get(NhaHang.class, maNhaHang);
        } catch (HibernateException ex) {
            System.err.println(ex);
        } finally {
            session.close();
        }
        return nh;
    }
    
    /**
     * @desc them nha hang
     * @param nh
     * @return 
     */
    public static boolean themNhaHang(NhaHang nh) throws HibernateException{
        Session session = HibernateUtil.getSessionFactory().openSession(); 
        if (NhaHangDAO.layThongTinNhaHang(nh.getMaNhaHang())!=null) { 
            return false; 
        } 
        Transaction transaction = null;
        boolean flag = true;
        try { 
            transaction = session.beginTransaction(); 
            session.save(nh);
            transaction.commit(); 
        } catch (HibernateException ex) { 
            //Log the exception 
            transaction.rollback(); 
            //System.err.println(ex);
            flag = false;
        } finally { 
            session.close();
        } 
        return flag;
    }
    
    /**
     * @desc cap nhat thong tin NhaHang
     * @param nh
     * @return 
     */
    public static boolean capNhatThongTinNhaHang(NhaHang nh) throws HibernateException{ 
        Session session = HibernateUtil.getSessionFactory().openSession(); 
        if (NhaHangDAO.layThongTinNhaHang(nh.getMaNhaHang()) == null) { 
            return false; 
        }
        boolean flag = true;
        Transaction transaction = null; 
        try { 
            transaction = session.beginTransaction(); 
            session.update(nh); 
            transaction.commit();
        } catch (HibernateException ex) { 
            //Log the exception 
            transaction.rollback(); 
            //System.err.println(ex); 
            flag = false;
        } finally { 
            session.close(); 
        }
        return flag; 
    } 
    
    /**
     * @desc xoa NhaHang
     * @param maNhaHang
     * @return 
     */
    public static boolean xoaNhaHang(int maNhaHang) throws HibernateException{ 
        Session session = HibernateUtil.getSessionFactory().openSession(); 
        NhaHang nh = NhaHangDAO.layThongTinNhaHang(maNhaHang); 
        if(nh==null){ 
            return false; 
        }
        boolean flag = true;
        Transaction transaction = null; 
        try { 
            transaction = session.beginTransaction(); 
            session.delete(nh); 
            transaction.commit(); 
        } catch (HibernateException ex) { 
            //Log the exception 
            transaction.rollback(); 
            //System.err.println(ex); 
            flag = false;
        } finally { 
            session.close(); 
        } 
        return flag; 
    }
}
